package com.aktk.battlenole;

import java.util.ArrayList;

import com.aktk.battlenole.GameData.LastResult;
import com.aktk.battlenole.GameData.Stages;
import com.aktk.gameelements.GamePlay;
import com.aktk.gameelements.Ship;
import com.aktk.gameelements.GamePlay.Orientation;
import com.aktk.gameelements.GamePlay.SegmentType;
import com.aktk.gameelements.Player;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class GameActivity extends Activity implements View.OnClickListener, BoardManager.OnClickListener {
	final int boardWidth = 10;
	final int boardHeight = 10;
	TextView message;
	//TextView playerShipStatus;
	TextView shipStatus;
	boolean shotFired;
	
	public enum BoardView {
		 PLAYER , OPPONENT;
	}
	BoardView view;
	
	GameData game;
	Player player;
	Player opponent;
	BoardManager manager;
	TextView bPlayerBoard;
	TextView bOpponentBoard;
	//Player currentTurn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        game = new GameData();
        shotFired = false;
        
        LinearLayout playerBoardContainer = (LinearLayout)this.findViewById(R.id.playerContainer);
        manager = new BoardManager (boardWidth, boardHeight);
        manager.setOnClickListener(this);
        manager.Draw(this, playerBoardContainer);
        
        player = game.getCurrentPlayer();
        opponent = game.getCurrentOpponent();
        
        message = (TextView)findViewById(R.id.messageLabel);
        message.setBackgroundColor(Color.parseColor("#90FFFFFF"));
        message.setTextColor(Color.BLACK);
        setMessage();
        
        bPlayerBoard = (TextView)findViewById(R.id.boardSelect1);
        bPlayerBoard.setText("My Board");
        bPlayerBoard.setOnClickListener(this);
        bPlayerBoard.setBackgroundColor( Color.BLACK );
        bPlayerBoard.setTextColor(Color.WHITE);
        bOpponentBoard = (TextView)findViewById(R.id.boardSelect2);
        bOpponentBoard.setText("Enemy Map");
        bOpponentBoard.setOnClickListener(this);
        bOpponentBoard.setBackgroundColor( Color.WHITE );
        bOpponentBoard.setTextColor(Color.BLACK);
        view = BoardView.OPPONENT;
        
        shipStatus = (TextView)findViewById(R.id.shipsRemaining);
        shipStatus.setText("Enemy ships remaining: " + Integer.toString(opponent.getShipsRemaining()));
        shipStatus.setBackgroundColor(Color.parseColor("#90FFFFFF"));
        shipStatus.setTextColor(Color.BLACK);
        mapRefresh();
    }

	@Override
	public void onClick(View v) {
		if( v.getId() == bPlayerBoard.getId() )
		{
			bPlayerBoard.setBackgroundColor( Color.WHITE );
			bPlayerBoard.setTextColor(Color.BLACK);
			bOpponentBoard.setBackgroundColor( Color.BLACK );
			bOpponentBoard.setTextColor(Color.WHITE);
			boardRefresh();
			view = BoardView.PLAYER;
		}
		
		if( v.getId() == bOpponentBoard.getId() )
		{
			bOpponentBoard.setBackgroundColor( Color.WHITE );
			bOpponentBoard.setTextColor(Color.BLACK);
			bPlayerBoard.setBackgroundColor( Color.BLACK );
			bPlayerBoard.setTextColor(Color.WHITE);
			mapRefresh();
			view = BoardView.OPPONENT;
		}
	}
	
	public void boardRefresh(){
		ArrayList<BoardCellState> states = manager.getStates();
		
		for (int index = 0; index < 100; index++)
		{
			if( player.incommingShots.getResult(index) == SegmentType.HIT )
			{
				states.get(index).setState(SegmentType.NONE, true, Orientation.HORIZONTAL);
			}
			else
			{
				states.get(index).setState(SegmentType.NONE, false, Orientation.HORIZONTAL);
			}
		}
		
		for (int a = 0; a < player.getFleetSize(); ++a )
		{
			Ship currentShip = player.getShip(a);
			if( currentShip.getUsed() )
			{
				for( int b = 0 ; b < currentShip.getLength() ; ++b )
				{
					int cellNumber = currentShip.getShipCellNumber( b, boardWidth );
					states.get( cellNumber ).setState(currentShip.getShipSegmentType(b), currentShip.getHit(b), currentShip.getOrientation());
				}
			}
		}
		shipStatus.setText("Your ships remaining: " + Integer.toString(player.getShipsRemaining()));
		manager.setStatus(states);

    }
	
	private void mapRefresh() {
		ArrayList<BoardCellState> states = manager.getStates();
		for (int index = 0; index < states.size(); index++)
		{
			states.get( index ).setState(player.opponentMap.getResult( index ) , Orientation.HORIZONTAL );
		}
		shipStatus.setText("Enemy ships remaining: " + Integer.toString(opponent.getShipsRemaining()));
		manager.setStatus(states);
	}
	
	private void setMessage() {
		if( game.lr == LastResult.HIT )
		{
			Toast.makeText(getApplicationContext(), "Your opponent made a direct Hit!", Toast.LENGTH_LONG).show();
		}
		if( game.lr == LastResult.MISS )
		{
			Toast.makeText(getApplicationContext(), "Your opponent missed.", Toast.LENGTH_LONG).show();
		}
		message.setText("Select 'Enemy Map' to fire a missile or 'My Board' to review your ships.");
	}
	
	@Override
	public void onClick(int row, int col) {
		if( view == BoardView.OPPONENT && shotFired == false && player.opponentMap.getResult(row, col) == SegmentType.NULL )
		{
			shotFired = true;
			if ( player.fireMissile( row,  col ) ) // Hit
			{
				player.opponentMap.setResult( row , col , SegmentType.HIT );
				opponent.incommingShots.setResult(row, col, SegmentType.HIT);
				mapRefresh();
				game.lr = LastResult.HIT;
				message.setText("Direct Hit!");
				Toast.makeText(getApplicationContext(), "Direct Hit", Toast.LENGTH_SHORT).show();
				new Handler().postDelayed(new Runnable() {
				    @Override
			        public void run() {
				    	Intent i = new Intent(GameActivity.this, SwitchActivity.class);
				    	startActivity (i);
			        }
			    }, 2000);
			}
			else // Miss
			{
				player.opponentMap.setResult( row , col , SegmentType.MISS );
				opponent.incommingShots.setResult(row, col, SegmentType.MISS);
				mapRefresh();
				game.lr = LastResult.MISS;
				message.setText("Miss.");
				Toast.makeText(getApplicationContext(), "Splash - Miss.", Toast.LENGTH_SHORT).show();
				new Handler().postDelayed(new Runnable() {
				    @Override
			        public void run() {
				    	Intent i = new Intent(GameActivity.this, SwitchActivity.class);
				    	startActivity (i);
			        }
			    }, 2000);
			}
		}
	}

}
